<template>
  <div>
    <el-button
      type="primary"
      style="margin-bottom: 20px"
      size="small"
      :disabled="!category3Id"
      @click="addSpu"
      >添加SPU</el-button
    >
    <el-table border :data="spuPageList">
      <el-table-column
        type="index"
        label="序号"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column label="SPU名称" prop="spuName"></el-table-column>
      <el-table-column label="SPU描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row, $index }">
          <el-button
            style="margin-right: 5px"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            title="添加SKU"
            @click="addSku(row)"
          ></el-button>
          <el-button
            style="margin-right: 5px"
            icon="el-icon-edit"
            type="warning"
            size="mini"
            title="修改SPU"
            @click="updateSpu(row)"
          ></el-button>
          <el-button
            style="margin-right: 5px"
            type="info"
            icon="el-icon-info"
            size="mini"
            title="查看SKU列表"
          ></el-button>
          <el-button
            style="margin-right: 5px"
            type="danger"
            icon="el-icon-delete"
            size="mini"
            title="删除SPU"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[3, 5, 7, 9]"
      :page-size="limit"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapState } from "vuex";
import spuApi from "@/api/spu";
import { Message } from "element-ui";
export default {
  name: "SpuList",
  computed: {
    ...mapState("category", ["category3Id"]),
  },
  data() {
    return {
      page: 1, //当前页
      limit: 3, //每页条数
      spuPageList: [], //spu分页列表数据
      total: 0,
    };
  },
  methods: {
    // 根据分页获取spu列表
    async getPageList() {
      const res = await spuApi.getPageList(
        this.page,
        this.limit,
        this.category3Id
      );
      if (res.code === 200) {
        this.spuPageList = res.data.records;
        this.total = res.data.total;
      } else {
        Message({
          type: "error",
          message: "获取spu分页列表失败",
        });
      }
    },
    // 添加SPU
    addSpu() {
      this.$emit("setShowState", 2);
    },
    // 修改SPU
    updateSpu(row) {
      this.$emit("setShowState", 2, row);
    },
    // 添加SKU
    addSku(row) {
      this.$emit("setShowState", 3, row);
    },
    // 根据页大小获取分页列表
    handleSizeChange(val) {
      this.limit = val;
      this.getPageList();
    },
    // 根据传入页码获取分页列表
    handleCurrentChange(val) {
      this.page = val;
      this.getPageList();
    },
  },
  watch: {
    category3Id: {
      handler(newVal) {
        if (!newVal) {
          this.spuPageList = [];
        } else {
          this.getPageList();
        }
      },
      immediate: true,
    },
  },
};
</script>

<style lang="scss" scoped></style>
